﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Diagnostics;

namespace WPFInterface.ViewModel
{
    public class LoginViewModel : BaseViewModel
    {
        #region variables
        //private DataAccess.Users _dataAccessUser = null;
        private bool _closeSignal;
        private string _login;
        private string _password;
        private string message;

        public string Message
        {
            get { return message; }
            set {
                if (message != value)
                {
                    message = value;
                    OnPropertyChanged("Message");
                }
            }
        }

        #endregion

        #region commandes
        private ICommand _loginCommand;
        #endregion

        #region getter / setter
        /// <summary>
        /// mot de passe de la personne
        /// </summary>
        public string Password
        {
            get { return _password; }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        /// <summary>
        /// login de la personne
        /// </summary>
        public string Login
        {
            get { return _login; }
            set
            {
                if (_login != value)
                {
                    _login = value;
                    OnPropertyChanged("Login");
                }

            }
        }

        /// <summary>
        /// indique si on doit fermer la fenêtre ou non
        /// </summary>
        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        /// <summary>
        /// command pour s'authentifier
        /// </summary>
        public ICommand LoginCommand
        {
            get { return _loginCommand; }
            set { _loginCommand = value; }
        }

        #endregion

        /// <summary>
        /// constructeur
        /// </summary>
        public LoginViewModel()
        {
            //init variables
            base.DisplayName = "Page de login";
            Login = "";
            Password = "";
            Message = "";

           // _dataAccessUser = new WPFInterface.DataAccess.Users();

            //commandes
            _loginCommand = new RelayCommand(param => LoginAccess(), param => true);
        }

        /// <summary>
        /// action permettant de s'authentifier
        /// </summary>
        private void LoginAccess()
        {
            ServiceUser.IServiceUser user = new ServiceUser.ServiceUserClient();
            if (user.Connect(Login, Password))
            {
                Message = "Loading...";
                TransitionEffects.SwirlTransitionEffect dte = new TransitionEffects.SwirlTransitionEffect();

                View.HomeView main = new View.HomeView();
                ViewModel.HomeViewModel vm = new HomeViewModel(Login);
                main.DataContext = vm;
                App.Current.MainWindow = main;
                this.CloseSignal = true;
                main.Effect = dte;
                main.Show();
            }
            else if (!user.Connect(Login, Password))
            {
                Message = "Incorrect user or password";
            }
             
        }
    }
}
